<?php

namespace app\data\controller\control;
use app\data\model\ControlClaimListModel;
use app\data\model\EnterStorehouseItemModel;
use app\data\model\EnterStorehouseModel;
use app\data\model\StorehouseModel;
use think\admin\Controller;
use think\facade\Db;

/**
 * 物资汇总
 * Class Goods
 * @package app\data\controller\shop
 */
class EnterSummary extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'sn_enter_storehouse_item';

    /**
     * 物资汇总
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '物资汇总';
        $query = $this->_query($this->table);
        // 列表排序并显示
        $query->where('status',1);
        //->dateBetween('enter_time')
        $query->equal('material_code,material_name,material_model,storehouse_id')->group('material_code')->field('*,sum(material_num) as material_num')->order('material_code asc')->page();
    }


    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {
        
        
        if(input('enter_time')){
            $enter_time = explode(' - ',input('enter_time'));
            // ->whereIn('material_code',array_keys($newData))
            $enter_storehouse_item = Db::name('sn_enter_storehouse_item')->whereBetween('enter_time',$enter_time)->group('material_code')->column('sum(material_num) as material_num,material_price,material_name','material_code');
        }else{
            $enter_storehouse_item = Db::name('sn_enter_storehouse_item')->group('material_code')->column('sum(material_num) as material_num,material_price,material_name','material_code');
        }
        $newData = array();
        foreach ($data as $k=>$v){
            $newData[$v['material_code']] = $v;
            if(isset($enter_storehouse_item[$v['material_code']])){
                $newData[$v['material_code']]['material_num'] = $enter_storehouse_item[$v['material_code']]['material_num'];
                $newData[$v['material_code']]['material_price'] = $enter_storehouse_item[$v['material_code']]['material_price'];
            }else{
                $newData[$v['material_code']]['material_num'] = 0;
                $newData[$v['material_code']]['material_price'] = 0;
            }
        }
        
        $ControlClaimListModel = new ControlClaimListModel();
        //获取出库数据
        if(input('enter_time')){
            $enter_time = explode(' - ',input('enter_time'));
            // ->whereIn('material_code',array_keys($newData))
            $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereBetween('plan_time',$enter_time)->whereIn('material_code',array_keys($newData))->group('material_code')->column('sum(material_num)','material_code');
        }else{
            $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->group('material_code')->whereIn('material_code',array_keys($newData))->column('sum(material_num)','material_code');
        }
        
        foreach ($newData as $ks=>$vs){
            $newData[$ks]['material_xjprice'] = sprintf("%01.2f", $vs['material_price'] * $vs['material_num']);
            if(!isset($claim_nums[$ks])){
                $newData[$ks]['claim_num'] = 0;
                $newData[$ks]['claim_xjprice'] = 0;
                $newData[$ks]['surplus_num'] = $newData[$ks]['material_num'];
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * $newData[$ks]['material_num']);
            }else{
                $newData[$ks]['claim_num'] = $claim_nums[$ks];
                $newData[$ks]['claim_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * $claim_nums[$ks]);
                $newData[$ks]['surplus_num'] = sprintf("%01.2f", $newData[$ks]['material_num'] - $claim_nums[$ks]);
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $newData[$ks]['material_price'] * sprintf("%01.2f", $newData[$ks]['material_num'] - $claim_nums[$ks]));
            }
        }
        
        //获取期初剩余数据
        $EnterStorehouseItemModel = new EnterStorehouseItemModel();
        if(input('enter_time')){
            $enter_time = explode(' - ',input('enter_time'));
            $start_nums = $EnterStorehouseItemModel->where('status',1)->whereIn('material_code',array_keys($newData))->group('material_code')->where('enter_time','<',$enter_time[0])->column('sum(material_num) as material_num,material_price','material_code');
        }else{
            // $start_nums = $EnterStorehouseItemModel->where('status',1)->whereIn('material_code',array_keys($newData))->where('enter_time','<',date('Y-m-d H:i:s',time()))->column('material_num','material_code');
            $start_nums = array();
        }
        foreach ($newData as $ks=>$vs){
            if(isset($start_nums[$ks]['material_code'])){
                $newData[$ks]['start_num'] = $start_nums[$ks]['material_num'];
                $newData[$ks]['start_xjprice'] = sprintf("%01.2f", $start_nums[$ks]['material_price'] * $start_nums[$ks]['material_num']);
                $newData[$ks]['surplus_num'] = sprintf("%01.2f", $newData[$ks]['surplus_num'] + $start_nums[$ks]['material_num']);
                $newData[$ks]['surplus_xjprice'] = sprintf("%01.2f", $start_nums[$ks]['material_price'] * $newData[$ks]['surplus_num']);
                $newData[$ks]['material_price'] = $start_nums[$ks]['material_price'];
            }
        }
        sort($newData);
        $data = $newData;
        $this->storehouse_list = StorehouseModel::where('deleted',0)->select()->toArray();
    }



}